<template>
  <div class="order-list-item">
    <div class="tit">
      <div class="time">{{ order.createDate }}</div>
      <div class="status">
        <span>{{ order.orderStatus }}</span>
      </div>
    </div>
    <div class="list">
      <div class="list-item" v-for="(cartItem, index) in order.orderDetail" :key="index">
        <div class="dish" v-if="cartItem.item.dishBasicDto !== null">
          <div class="goods-img">
            <img :src="cartItem.item.dishBasicDto.image" alt="">
          </div>
          <div class="goods-content text-ellipsis-2">
            {{ cartItem.item.dishBasicDto.dishName }}&nbsp;{{ cartItem.item.dishBasicDto.description }}
          </div>
          <div class="goods-trade">
            <p>¥ {{ cartItem.item.totalMoney }}</p>
            <p>x {{ cartItem.item.num }}</p>
          </div>
        </div>
        <div class="setmeal" v-if="cartItem.item.setmealBasicDto !== null">
          <div class="goods-img">
            <img :src="cartItem.item.setmealBasicDto.image" alt="">
          </div>
          <div class="goods-content text-ellipsis-2">
            {{ cartItem.item.setmealBasicDto.setmealName }}&nbsp;{{ cartItem.item.setmealBasicDto.description }}
          </div>
          <div class="goods-trade">
            <p>¥ {{ cartItem.item.totalMoney }}</p>
            <p>x {{ cartItem.item.num }}</p>
          </div>
        </div>
      </div>
    </div>
    <div class="total">
      共{{ totalSize }}件商品，总金额 ¥ {{ order.totalMoney }}
    </div>
    <div class="actions">
      <span @click="viewOrder">查看详情</span>&nbsp;
      <span @click="editStatus" v-if="item.orderStatus !== '已完成'">确认收货</span>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data () {
    return {
      totalSize: 0,
      order: {},
      orderType: [
        {
          value: 1,
          label: '已支付'
        },
        {
          value: 2,
          label: '已发货'
        },
        {
          value: 3,
          label: '已完成'
        }
      ]
    }
  },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    getOrderById () {
      request({
        url: `/order-basic/order-basic/${this.item.id}`,
        method: 'get'
      }).then(res => {
        this.order = res.data.data
        this.order.orderDetail = JSON.parse(this.order.orderDetail)
        this.order.orderDetail.forEach(it => {
          this.totalSize += it.item.num
        })
        this.order.orderStatus = this.changeStatusTostring(this.order.orderStatus)
      })
    },
    editStatus () {
      request({
        url: '/order-basic/order-basic',
        method: 'put',
        data: {
          id: this.item.id,
          orderStatus: 3
        }
      }).then(res => {
        if (res.data.data) {
          this.getOrderById()
        }
        this.$emit('changeList')
      })
    },
    viewOrder () {
      this.$router.push({
        name: 'OrderDetail',
        params: {
          or: this.order
        }
      })
    },
    changeStatusTostring (order) {
      let label = ''
      this.orderType.forEach(item => {
        if (item.value === order) {
          label = item.label
        }
      })
      return label
    }
  },
  created () {
    this.getOrderById()
  }
}
</script>

<style lang="less" scoped>
.order-list-item {
  margin: 10px auto;
  width: 94%;
  padding: 15px;
  background-color: #ffffff;
  box-shadow: 0 0.5px 2px 0 rgba(0,0,0,.05);
  border-radius: 8px;
  color: #333;
  font-size: 13px;

  .tit {
    height: 24px;
    line-height: 24px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .status {
      color: #fa2209;
    }
  }
  .list-item {
    .dish,.setmeal {
    display: flex;
    .goods-img {
      width: 90px;
      height: 90px;
      margin: 0px 10px 10px 0;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .goods-content {
      flex: 2;
      line-height: 18px;
      max-height: 36px;
      margin-top: 8px;
    }
    .goods-trade {
      flex: 1;
      line-height: 18px;
      text-align: right;
      color: #b39999;
      margin-top: 8px;
    }
  }
  }

  .total {
    text-align: right;
  }
  .actions {
    text-align: right;
    span {
      display: inline-block;
      height: 28px;
      line-height: 28px;
      color: #383838;
      border: 0.5px solid #a8a8a8;
      font-size: 14px;
      padding: 0 15px;
      border-radius: 5px;
      margin: 10px 0;
    }
  }
}
</style>
